package cn.yj.common.service;

import cn.yj.common.emums.PayType;
import cn.yj.common.factory.SignFactory;
import cn.yj.common.interfaces.IPayRefundSuccessNotify;
import cn.yj.commons.utils.MapUtils;
import cn.yj.commons.utils.StringUtils;
import cn.yj.tools.exception.ServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 处理异步结果通知
 *
 * @author 永健
 * @since 2022-02-19 13:45
 */
@Component
public class PayRefundSuccessNotifyHandler {

    private static final Logger log = LoggerFactory.getLogger(PayRefundSuccessNotifyHandler.class);

    @Autowired(required = false)
    private List<IPayRefundSuccessNotify> notifies;

    /**
     * 下单的时候主动去查询订单支付状态-且成功的通知
     * 或者是前端调起支付，付款后的主动请求回调
     *
     * @param payType    微信还是支付宝
     * @param outTradeNo 商户订单号
     */
    public void syncNotify(PayType payType, boolean success, String outTradeNo) {
        Map<String, Object> params = new HashMap<>();
        params.put("outTradeNo", outTradeNo);
        this.loopNotifyListener(payType, false, false, outTradeNo);
    }


    private void loopNotifyListener(PayType payType, boolean success, boolean tag, String outTradeNo) {
        if (StringUtils.isNull(notifies)) {
            return;
        }
        for (IPayRefundSuccessNotify notify : notifies) {
            switch (payType) {
                case ALIPAY:
                    notify.alipay(outTradeNo, success, tag);
                    break;
                case WECHAT:
                    notify.wechat(outTradeNo, success, tag);
                    break;
                default:
                    break;
            }
        }
    }
}
